﻿/************************************************************************
* Copyright (c) 2018 All Rights Reserved.
*命名空间：Common.Standard
*文件名： AssemblyHelper
*创建人： kingling
*创建时间：2018/7/6 16:41:47
*描述
*=======================================================================
*修改标记
*修改时间：2018/7/6 16:41:47
*修改人：kingling
*描述：
************************************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace Common.Standard
{
    public class AssemblyHelper
    {
        /// <summary>
        /// 动态创建实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="assemblyName"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public static T CreateInstance<T>(string assemblyName, string typeName) where T : class
        {
            var ass = GetAssembly(assemblyName);
            Type type = ass.GetType(assemblyName + "." + typeName);
            //创建
            T t = (T)Activator.CreateInstance(type);
            return t;
        }
        /// <summary>
        /// 获取程序集
        /// </summary>
        /// <param name="assemblyName"></param>
        /// <returns></returns>
        public static Assembly GetAssembly(string assemblyName) {
            var path = AppDomain.CurrentDomain.BaseDirectory;
            //使用byte[] 参数加载程序集，这样不占用程序集文件。
            byte[] fileData = File.ReadAllBytes(path + assemblyName + ".dll");
            var ass = Assembly.Load(fileData);
            return ass;
        }
    }
}
